package com.dingwei.lesyndrome.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Adapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingwei.lesyndrome.R;
import com.dingwei.lesyndrome.adapter.MyNewAdapter;
import com.dingwei.lesyndrome.bean.MyNewsBean;
import com.dingwei.lesyndrome.mvp.presenter.MyNewsPresenter;
import com.dingwei.lesyndrome.mvp.view.Contract;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.DisplayUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/2/26.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description : 我的消息
 */

public class MyNewsActivity extends BaseActivity implements Contract.MyNewsView {
    MyNewsPresenter myNewsPresenter = new MyNewsPresenter(this);

    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.img_no_data)
    ImageView imgNoData;
    @Bind(R.id.tv_no_data)
    TextView tvNoData;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;

    MyNewAdapter mAdapter;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_my_news;
    }

    @Override
    protected void initData() {
        refreshLayout.autoRefresh();
    }

    @Override
    protected void initView() {

        imgNoData.setImageResource(R.mipmap.img_msg_no);
        tvNoData.setText("主人！还没有收到消息呢！");
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.addItemDecoration(new DividerDecoration(getResources().getColor(R.color.bg1), DisplayUtil.dip2px(BaseActivity.context, 8)));

        mAdapter = new MyNewAdapter(BaseActivity.context);
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                intent = new Intent(BaseActivity.context, WebViewActivity.class);
                MyNewsBean.DataBean bean = mAdapter.getItem(position);
                intent.putExtra("title","消息详情");
                intent.putExtra("url",bean.weburl);
                startActivity(intent);
            }
        });
        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page = page + 1;
                myNewsPresenter.initData(page, false, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                myNewsPresenter.initData(page, true, false);
            }
        });
    }

    public void showData(String state) {
        if (state.equals("success")) {
            noNetLayout.setVisibility(View.GONE);
            refreshLayout.setVisibility(View.VISIBLE);
            if (mAdapter.getAllData() != null && mAdapter.getAllData().size() > 0) {
                mRecyclerView.setVisibility(View.VISIBLE);
                noDataLayout.setVisibility(View.GONE);
            } else {
                mRecyclerView.setVisibility(View.GONE);
                noDataLayout.setVisibility(View.VISIBLE);
            }
        } else {
            refreshLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onFail() {
        refreshLayout.finishLoadmore();
        refreshLayout.finishRefresh();
        showData("fail");
    }

    @Override
    public void onRefresh(MyNewsBean bean) {
        mAdapter.clear();
        mAdapter.addAll(bean.data);

        refreshLayout.finishRefresh();

        showData("success");
    }

    @Override
    public void onLoadMore(MyNewsBean bean) {
        mAdapter.addAll(bean.data);

        refreshLayout.finishLoadmore();

        showData("success");
    }

    @OnClick(R.id.no_net_btn)
    public void onViewClicked() {
        page = 1;
        myNewsPresenter.initData(page, true, true);
    }
}
